<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Author: ThanhTV
* Email: thanhtran@vietnambiz.com
* Date create: Web,24/08/2011
* Description: Products controller
*/
require_once('admin_controller'.EXT);

class company extends admin_controller
{
    var $data = array();

    function company()
    {
        $this->load->library('pagination');
		parent::admin_controller();
		$this->load->model(array("company_model"));        
    }

    function index()
    {
        $this->data["all_company"] = $this->company_model->get_all_company(1,20);
        $this->data['tpl_view'] = 'company/index';
        $this->load->view('admin_layout',$this->data);
    }

    function list_ajax($page = 1)
    {
        //pagination
        $limit 		= ($this->input->post('limit') > 0) ? $this->input->post('limit') : 20;
        $num_rows 	= $this->company_model->get_count_all_company();
        $total 		= ceil($num_rows/$limit);
        $url 		= base_url_admin('company/list_ajax');
        $this->session->set_userdata('url_list_company',$url . '/' . $page);
        $conf = array(
            'cur_page'          =>	$page,
            'total_rows'		=>	$total,
            'base_url'          =>  $url,
            'js_function'		=>	'list_ajax'
        );
        $this->pagination->initialize($conf);
        $this->data['pagnav'] = $this->pagination->ajax_url();
        $this->data['limit']  = $limit;
        //end pagination

        $this->data["all_company"] = $this->company_model->get_all_company($page,$limit);
        
        $this->load->view('company/list_ajax',$this->data);
    }

    function add()
    {    	
        if($_SERVER["REQUEST_METHOD"]=="POST")
        {
        	$this->form_validation->set_rules('name', 'name', 'required');
            if($this->form_validation->run() == TRUE)
            {
	            $name   = $this->input->post('name');	           
	            
	            if($this->company_model->check_name($name)==FALSE)
	            {                
	                $arr_in = array(
	                    'name'                 	=> $name,
	                	'name_link'            	=> create_link($name),
	                	'order'                 => $this->company_model->get_max_order() + 1,     		                
	                    'status'               	=> $this->input->post('status')
	                );
	    	
	                $re = $this->db->insert('company', $arr_in);
	            
	                if(!$re)
	                    {die("not_ok");}
	                    else{die("ok");}
	             }
	             else{
	                    die("name_exits");
	                 }
            }

        }
        $this->load->view('company/add',$this->data);
    }

    function edit()
    {
        $act = $this->input->post('act');
        $id  = $this->input->post("id");
        
        if($act == 'process')
        {
            $name  = $this->input->post('name');
			
            if($this->company_model->check_name_edit($name,$id)==FALSE)
            {            
               $arr_in = array(
                    'name'                 => $name,
                	'name_link'            => create_link($name),                	                                       
                    'status'               => $this->input->post('status')
                );

                $re = $this->db->where('id', $id)->update('company', $arr_in);
                die("ok");
            }
            else{ die("name_exits"); }
        }
        $this->data["company_info"] = $this->company_model->get_company_info($id);
        $this->load->view('company/edit',$this->data);
    }

    function change_status()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST'){

            $active  = $this->input->post('mode');
            $id = $this->input->post('id');

            $this->db->where_in('id', $id);
            $this->db->update('company', array('status' => $active));

            die($active);
        }
    }   

    function change_multi_status()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $active  = $this->input->post('sel_action');
            $id = $this->input->post('id');
            $this->db->where_in('id', $id);
            $this->db->update('questions_subjects', array('status' => $active));

            die($active);
        }
    }

    function del($id)
    {
        if(!$this->company_model->del_company($id))
        {die('no');}
        die('yes');
    }

    function change_order()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $cats = $this->input->post('lib');

            foreach($cats as $cat)
            {
                if($cat['old_order'] != $cat['order'])
                {
                    $this->company_model->change_order($cat['id'], $cat['old_order'], $cat['order']);
                }
            }
            die(1);
        }
    }
}